/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.workplan;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.workplan.Workplan;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-03-20
 */
@Service("workplanService")
public class WorkplanServiceImpl implements WorkplanService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Workplan
     ****************************************/
    public void insert(Workplan workplan) throws Exception {
        workplan.setPlanid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(workplan);
    }

    /****************************************
     * Update a instace of Workplan
     ****************************************/
    public void update(Workplan workplan) throws Exception {
        iDataSet4iBatis.update(workplan);
    }

    /****************************************
     * Delete a instace of Workplan
     ****************************************/
    public void delete(Workplan workplan) throws Exception {
        iDataSet4iBatis.delete(workplan);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Workplan workplan) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Workplan workplan) throws Exception {
        String statementId = workplan.getStatmentId();
        String mainId = workplan.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> workplanList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Workplan model = new Workplan();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            workplanList.add(model);
        }
        iDataSet4iBatis.batchDelete(workplanList);
    }

    /****************************************
     * Query instaces of Workplan
     ****************************************/
    public List<BaseModel> queryAsList(Workplan workplan) throws Exception {
        String queryListStatement = "";
        if (workplan.isPaging()) {
            queryListStatement = workplan.getStatmentId();
            workplan.setStatmentId("Workplan.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(workplan);
            workplan.setTotalCount(baseModel.getTotalCount());
            workplan.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(workplan);
    }

    /****************************************
     * Query a instaces of Workplan
     ****************************************/
    public BaseModel queryAsObject(Workplan workplan) throws Exception {
        return iDataSet4iBatis.queryAsObject(workplan);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
